#include "Std_Types.h"
#include "Platform_Lib.h"
#include "RegTC1782.h"

#pragma section code "libc"
#pragma align 4
/*******************************************************************
| function implementations (scope: module-exported)
|------------------------------------------------------------------*/

/*******************************************************************************
** Syntax : void Mcal_ResetENDINIT(void)                                      **
**                                                                            **
** Service ID:  None                                                          **
**                                                                            **
** Sync/Async:  Synchronous                                                   **
**                                                                            **
** Reentrancy:  Non reentrant                                                 **
**                                                                            **
** Parameters (in) :  None                                                    **
**                                                                            **
** Parameters (out):  None                                                    **
**                                                                            **
** Return value    :  None                                                    **
**                                                                            **
** Description : Clears the ENDINIT bit in the                                **
**               WDT_CON0 register in order to disable the                    **
**               write-protection for registers protected                     **
**               via the EndInit feature (ie. WDT_CON1, BTV,                  **
**               BIV, ISP, mod_CLC).                                          **
**                                                                            **
*******************************************************************************/
void Mcal_ResetENDINIT(void)
{
  uint32 uvalcon0 ;
  volatile uint32 ReadBack;
  DEFINECRITICAL();
  /*
   1st step :
   Unlock the Register; following bit pattern is defined.
   bit 0 : EndInit Status, bit 1: 0, bit2,bit 3 (WDT_CON1 (TIR and TDR status)
   bit 4-7:1111 bit8 - 15 : PW rest reload value
  */

  /* 
   read contents of WDT_CON0 
  */
  uvalcon0 = WDT_CON0.U ;
  /*
   mask hardware passwd 
  */ 
  uvalcon0 &= 0xFFFFFF01U ;
  /*
   set WDTHPW1 
  */ 
  uvalcon0 |= 0x000000F0U ; 
  /*
   load copy of WDTCON1_WDTDR and WDTCON1_WDTIR
  */
  uvalcon0 |= (WDT_CON1.U & 0x0000000CU) ;
  /*
   Interrupts register should be saved and restored
  */
  ENTER_CRITICAL();
  /*
   set WDT_CON0
  */
  WDT_CON0.U = uvalcon0 ;
  
  /*
   2nd step :
   bit0 desired endinit; bit 1 : 1 rest remains the same
  */
  /*
   mask WDTHPW0, WDTCLK, ENDINIT
  */
  uvalcon0 &= 0xFFFFFFF0U ;
  /*
   set WDTHPW0=0,WDTCLK=1,ENDINIT=0
  */
  uvalcon0 |= 0x00000002U;
  /*
   set WDT_CON0    
  */
  WDT_CON0.U = uvalcon0 ;
  /* 
     Read Back WDT_CON0 register as per User 
     Manual Addendum Document (Chapter 14-4) 
  */
  ReadBack = WDT_CON0.U ;
  EXIT_CRITICAL();
}

/*******************************************************************************
** Syntax : Mcal_SetENDINIT(void)                                             **
**                                                                            **
** Service ID:  None                                                          **
**                                                                            **
** Sync/Async:  Synchronous                                                   **
**                                                                            **
** Reentrancy:  Non reentrant                                                 **
**                                                                            **
** Parameters (in) :  None                                                    **
**                                                                            **
** Parameters (out):  None                                                    **
**                                                                            **
** Return value    :  None                                                    **
**                                                                            **
** Description :      Bit ENDINIT in WDTCON0 is set                           **
**                    description: Clears the ENDINIT bit in the              **
**                    WDT_CON0 register in order to disable the               **
**                    write-protection for registers protected                **
**                    via the EndInit feature (ie. WDT_CON1, BTV,             **
**                    BIV, ISP, mod_CLC).                                     **
**                                                                            **
*******************************************************************************/
void  Mcal_SetENDINIT(void)
{
  uint32 uvalcon0;
  DEFINECRITICAL();
  /*
   1st step :
  */
  /*
   read contents of WDT_CON0
  */
  uvalcon0 = WDT_CON0.U ;
  /*
   mask hardware passwd
  */
  uvalcon0 &= 0xFFFFFF01U ;    
  /*
   set WDTHPW1
  */
  uvalcon0 |= 0x000000F0U ;    
  /*
   load copy of WDTCON1_WDTDR and WDTCON1_WDTIR
  */
  uvalcon0 |= (WDT_CON1.U & 0x0CUL) ;

  /*
   Interrupts register should be saved and restored
  */
  ENTER_CRITICAL();
  /*
   set WDT_CON0
  */
  WDT_CON0.U = uvalcon0 ;

  /*
   2nd step :
  */
  /*
   mask WDTHPW0, WDTCLK, ENDINIT
  */
  uvalcon0 &= 0xFFFFFFF0U ;
  /*
   set WDTHPW0=0,WDTCLK=1,ENDINIT=1
  */
  uvalcon0 |= 0x00000003U;
  /*
   set WDT_CON0
  */
  WDT_CON0.U = uvalcon0 ;
  EXIT_CRITICAL();
}
